package com.example.demo.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.auth0.jwt.JWT;

import com.example.demo.Aspect.Logging;
import com.example.demo.GeneralResponse.APIResponse;
import com.example.demo.Service.EmployeeService;
import com.example.demo.Service.HRService;
import com.example.demo.Service.UserService;

/**
 * @version: V0.3
 * @author: 郑皓杰 吴嘉鑫
 * @className: QueryController
 * @packageName: com.example.demo.Controller
 * @description: 这是用户进行信息查询的控制类
 * @data: 2021-07-14
 **/


@RequestMapping("/Query")
@RestController
public class QueryController {
	@Autowired
	private EmployeeService empService;
	@Autowired
	private HRService hrService;
	@Autowired
	private UserService userService;

	/**
	* @author:郑皓杰
	* @methodsName: hrquery
	* @description: HR在全局通过name或者telephone查找已注册员工
	* @param:  
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/QueryHr")
	public APIResponse<?> hrquery(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {	
		Integer page=Integer.parseInt(httpServletRequest.getParameter("page"));
		return APIResponse.success("successquery",hrService.queryEmpByNameOrTel(httpServletRequest.getParameter("nameortel"),page));
	}
	
	
	/**
	* @author:吴嘉鑫
	* @methodsName: queryOne
	* @description: HR在查询某个人的详细经历
	* @param:  
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/searchone")
	//查询某个人
	public APIResponse<?> queryOne(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		String tel = httpServletRequest.getParameter("tel");
		return APIResponse.success("successquery",hrService.queryDesBytel(tel));
	}
	
	/**
	* @author:吴嘉鑫
	* @methodsName: queryPages
	* @description: 查询页数
	* @param:  
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/queryPage")
	public APIResponse<?> queryPages(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		String nameortel = httpServletRequest.getParameter("nameortel");
		return APIResponse.success("successquery",hrService.pagessum(nameortel));
	}
	
	/**
	* @author:吴嘉鑫
	* @methodsName: authorityQuery
	* @description: 确认HR权限，即HR是否在查看本公司员工经历
	* @param:  
	* @return: APIResponse
	* @throws: 
	
	@CrossOrigin
	@RequestMapping("/authorityquery")
	//确认权限
	public APIResponse<?> authorityQuery(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
		//Integer companyID = ((HumanResource)httpServletRequest.getSession().getAttribute("hr")).getCompanyID();
		String token = httpServletRequest.getHeader("Token");
		Integer companyID = tokenService.getCompanyIDfromToken(token);
		Integer desid=Integer.parseInt(httpServletRequest.getParameter("desid"));
		return APIResponse.success(hrService.queryAuthority(companyID, desid));
	}
	*/
	
	/**
	* @author:吴嘉鑫
	* @methodsName: authorityQuery
	* @description: 通过返回description id某个experience
	* @param:  desid
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/searchexp")
	//返回exp
	public APIResponse<?> queryExpByexpid(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
		Integer desid=Integer.parseInt(httpServletRequest.getParameter("desid"));
		return hrService.queryExpByexpid(desid);
	}
	
	/**
	* @author:郑皓杰
	* @methodsName: queryByState1
	* @description: 查询系统内当前为自由人的员工
	* @param:  HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/hrsearchbystate")
	public APIResponse<?> queryByState1(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		return APIResponse.success("successquery",hrService.searchFree());
	}
	
	/**
	* @author:郑皓杰
	* @methodsName: queryByState2
	* @description: 查询系统内当前处于招人状态的公司
	* @param:  HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/empsearchbystate")
	public APIResponse<?> queryByState2(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		return APIResponse.success("successquery",empService.searchFree());
	}
	
	/**
	* @author:郑皓杰
	* @methodsName: queryMyself
	* @description: 查询员工自我信息
	* @param:  HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/empsearchmyself")
	public APIResponse<?> queryMyself(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		return empService.getInfo(JWT.decode(httpServletRequest.getHeader("Token")).getClaim("username").asString());
	}
	
	/**
	* @author:郑皓杰
	* @methodsName: queryPerformance
	* @description: hr获取绩效
	* @param:  HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/hrqueryPerformance")
	public APIResponse<?> hrGetPerformance(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		 Integer expid=Integer.parseInt(httpServletRequest.getParameter("expid"));
		return hrService.getPerformance(expid);
	}
	
	/**
	* @author:郑皓杰
	* @methodsName: queryPerformance
	* @description: emp获取绩效
	* @param:  HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/empqueryPerformance")
	public APIResponse<?> empGetPerformance(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		 Integer empid=Integer.parseInt(httpServletRequest.getParameter("empid"));
		return empService.getNowPerformance(empid);
	}
	
	/**
	* @author:郑皓杰
	* @methodsName: queryPerformance
	* @description: 获取经历
	* @param:  HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/queryExperience")
	public APIResponse<?> getExperience(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		return APIResponse.success("successget",empService.getExp(JWT.decode(httpServletRequest.getHeader("Token")).getClaim("username").asString()));
	}
	
	
	/**
	* @author:郑皓杰
	* @methodsName: queryByAmbiguousKey
	* @description: 模糊查询职位
	* @param:  HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@Logging
	@CrossOrigin
	@RequestMapping("/queryJob")
	public APIResponse<?> queryByAmbiguousKey(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		String keyword=httpServletRequest.getParameter("Keyword");
		Integer page=Integer.parseInt(httpServletRequest.getParameter("Page"));
		return hrService.searchByJobKey(keyword,page);
	}
	
	/**
	* @author:吴嘉鑫
	* @methodsName: queryMessage
	* @description: 查询自己信息
	* @param:  HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/queryMessage")
	public APIResponse<?> queryMessage(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		String username = JWT.decode(httpServletRequest.getHeader("Token")).getClaim("username").asString();
		return userService.getMyMessage(username);
	}
	
	/**
	* @author:吴嘉鑫
	* @methodsName: queryMyself
	* @description: 分析员工
	* @param:  HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/queryMine")
	public APIResponse<?> queryMine(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		 Integer empid=Integer.parseInt(httpServletRequest.getParameter("empid"));
		return empService.getPastSalary(empid);

	}

}
